#include "rbtree.hpp"

namespace bit
{
    using namespace ns_RBTree;

    template <class K, class V>
    class map
    {
    public:
        struct MapKeyOfT
        {
            const K &operator()(const std::pair<K, V> &kv)
            {
                return kv.first;
            }
        };

        typedef typename RBTree<K, std::pair<K, V>, MapKeyOfT>::iterator iterator;

        std::pair<iterator, bool> insert(const std::pair<K, V> &kv)
        {
            return rb.insert(kv);
        }

        iterator Find(const K& key)
        {
            return rb.Find(key);
        }

        iterator begin()
        {
            return rb.begin();
        }

        iterator end()
        {
            return rb.end();
        }

        V& operator[](const K& key)
        {
            auto ret = rb.insert(std::make_pair(key, V()));
            return ret.first->second;
        }   

    private:
        RBTree<K, std::pair<K, V>, MapKeyOfT> rb;
    };

    void Test()
    {
        map<int, int> m;
        m.insert(std::make_pair(13,13));
        m.insert(std::make_pair(8,8));
        m.insert(std::make_pair(17,17));
        m.insert(std::make_pair(1,1));
        m.insert(std::make_pair(11,11));
        m.insert(std::make_pair(15,15));
        m.insert(std::make_pair(25,25));
        m.insert(std::make_pair(6,6));
        m.insert(std::make_pair(22,22));
        m.insert(std::make_pair(27,27));
        
        auto lt = m.begin();
        while(lt != m.end())
        {
            std::cout << lt->first << " : " << lt->second << std::endl;
            ++lt;
        }
    }
}